var express = require('express'),
   routes = require('./routes'),
   http = require('http'),
   path = require('path'),
   controllers = require('./controllers'),
   User = require('./models').User;

var app = express();
app.configure(function() {
   app.set('port', process.env.PORT || 3000);
   app.set('views', __dirname + '/views');
   app.set('view engine', 'ejs');
   app.set('layout', 'layout');
   app.use(express.favicon());
   app.use(express.logger('dev'));
   app.use(express.bodyParser({uploadDir:'./uploads'}));
   app.use(express.cookieParser());
   app.use(express.methodOverride());
   app.use(express.static(path.join(__dirname, 'public')));

   app.use(User.Verify);
   app.use(function(req, res, next) {
      res.locals.cssSection = [];
      res.locals.scriptSection = [];
      res.locals.renderScript = function() {
         return res.locals.scriptSection.map(function(script) {
            return '<script src="' + script + '"></script>';
         }).join('\n ');
      }

      res.locals.renderCSS = function() {
         console.log(res.locals.cssSection)
         return res.locals.cssSection.map(function(css) {
            return '<link rel="stylesheet" type="text/css" href="' + css + '" />'
         }).join('\n ');
      }

      res.locals.views = {
         auth: function() {
            return req.verifyUser;
         }
      }
      next();
   });
});

app.configure('development', function() {
   app.use(express.errorHandler());
});
// app.get('/', controllers.usermanage.edit);
app.get('/', routes.index);
app.get('/i', routes.i);
app.get('/account/reg', controllers.account.reg);
app.get('/account/signin', controllers.account.sign);
app.post('/account/register', controllers.account.register);
app.post('/account/signin', controllers.account.signin);
app.get('/usermanage/edit', controllers.usermanage.edit);

app.get('/markup', routes.markup);
app.post('/fileupload',controllers.usermanage.fileupload);
app.get('/usermanage/preview',controllers.usermanage.preview);
app.get('/usermanage/home',controllers.usermanage.home);
app.get('/usermanage/article',controllers.usermanage.article);
app.get('/usermanage/people',controllers.usermanage.people);
app.get('/usermanage/setting',controllers.usermanage.setting);


module.exports = app;

http.createServer(app).listen(app.get('port'), function() {
   console.log("Express server listening on port " + app.get('port'));
});